//
//  Crazy_TimeChange.m
//  webSocket
//
//  Created by 马军凯 on 2019/1/15.
//  Copyright © 2019年 ZH. All rights reserved.
//

#import "Crazy_TimeChange.h"

@implementation Crazy_TimeChange

// 当前时间转换为时间戳
+ (NSString *)currentTimeChangeTimeString {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss SSS"]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate *nowDate = [NSDate date];
    //时间转时间戳的方法:
    NSInteger timeSp = [[NSNumber numberWithDouble:[nowDate timeIntervalSince1970] * 1000] integerValue];
    return [NSString stringWithFormat:@"%ld", timeSp];
}

//时间戳转化为时间
+ (NSString *)timeStringChangeTime:(NSString *)timeString {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    NSTimeInterval time = [timeString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    
    return [formatter stringFromDate:date];
}


@end
